const fs = require("fs");
const SWPrecacheWebpackPlugin = require("sw-precache-webpack-plugin");
const path = require("path");
function resolve(dir) {
  return path.join(__dirname, dir);
}
module.exports = {
  // 修改的配置
  // 将baseUrl: '/api',改为baseUrl: '/',
  baseUrl: "/",
  devServer: {
    // port: 8083,
    proxy: {
      "/api": {
        target: "https://hn2.api.okayapi.com",
        changeOrigin: true,
        ws: true,
        pathRewrite: {
          "^/api": "/"
        }
      },
      "/okApi": {
        target: "https://api.okayapi.com",
        changeOrigin: true,
        ws: true,
        pathRewrite: {
          "^/okApi": "/"
        }
      }
    }
  },
  pwa: {
    plugins: [
      new SWPrecacheWebpackPlugin({
        cacheId: "my-vue-app",
        filename: "service-worker.js",
        staticFileGlobs: ["dist/**/*.{js,html,css}"],
        minify: true,
        stripPrefix: "dist/"
      })
    ]
  },
  productionSourceMap: true,
  css: {
    // 是否使用css分离插件 ExtractTextPlugin
    extract: true,
    // 开启 CSS source maps?
    sourceMap: true,
    // css预设器配置项
    loaderOptions: {
      sass: {
        data: fs.readFileSync("src/assets/css/common.scss", "utf-8")
      }
    },
    // 启用 CSS modules for all css / pre-processor files.
    modules: false
  },
  chainWebpack: config => {
    config.resolve.alias.set("@", resolve("src")); // key,value自行定义，比如.set('@@', resolve('src/components'))
  }
};
